package com.google.project.server;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.project.client.Service.TaskConstrainService;
import com.google.project.server.entity.ProjectTask;
import com.google.project.server.entity.TaskConstrain;
import com.google.project.shared.ProjectTaskDTO;
import com.google.project.shared.TaskConstrainDTO;

@SuppressWarnings("serial")
public class TaskConstrainServiceImpl extends RemoteServiceServlet implements TaskConstrainService {

	@Override
	public TaskConstrainDTO createContrain(TaskConstrainDTO data,String taskId) {
		// TODO Auto-generated method stub
		PersistenceManager pm = PMF.get().getPersistenceManager();
		 TaskConstrain constrain = null;
		 ProjectTask task =   pm.getObjectById(ProjectTask.class, taskId);
		 data.setForTaskId(task.getTaskId());
		 constrain  = new TaskConstrain(data);
		 task.getListOfConstrain().add(constrain);
		 pm.makePersistent(task);
		 updateCurrentTask(task.getTaskId(),constrain.getConstrainTaskId());
		 updateConstrainTask(constrain.getConstrainTaskId(),task.getTaskId());
		return constrain.toDTO(); 
	}
   
	void updateConstrainTask(int taskId,int currentTaskId)
	{
		ProjTaskServiceImpl taskServer = new ProjTaskServiceImpl();
		ProjectTaskDTO task = taskServer.getTaskByTaskId(taskId);
		if(task.getDependent()!=null&&task.getDependent().length()>0)
		{
			String afterData = task.getDependent().substring(task.getDependent().indexOf("|"));
			if(afterData.length()>1)
			{
				afterData=afterData.substring(1);
			}
			else
			{
				afterData="";
			}
			if(afterData!=null&&!afterData.equalsIgnoreCase(""))
			{
				task.setDependent(task.getDependent()+","+currentTaskId);
			}else
			{
				task.setDependent(task.getDependent()+currentTaskId);
			}
		}
		else
		{
			task.setDependent("|"+currentTaskId);
		}
		
		taskServer.updateTask(task);
		
	}
	
	void updateCurrentTask(int taskId,int constrainTaskId)
	{
		ProjTaskServiceImpl taskServer = new ProjTaskServiceImpl();
		ProjectTaskDTO task = taskServer.getTaskByTaskId(taskId);
		if(task.getDependent()!=null&&task.getDependent().length()>0)
		{
//			String[] check =task.getDependent().split("|");
			String preData =  task.getDependent().substring(0, task.getDependent().indexOf("|"));
			if(preData!=null&&!preData.equalsIgnoreCase(""))
			{
				task.setDependent(constrainTaskId+","+task.getDependent());
			}else
			{
				task.setDependent(constrainTaskId+task.getDependent());
			}
			
		}
		else
		{
			task.setDependent(constrainTaskId+"|");
		}
		
		taskServer.updateTask(task);
		
	}
	
	
	void updateConstrainTaskForDelete(int taskId,int currentTaskId)
	{
		ProjTaskServiceImpl taskServer = new ProjTaskServiceImpl();
		ProjectTaskDTO task = taskServer.getTaskByTaskId(taskId);
		String newAfter="";
		if(task.getDependent()!=null&&task.getDependent().length()>0)
		{
			String preData =  task.getDependent().substring(0, task.getDependent().indexOf("|"));
			String afterData = task.getDependent().substring(task.getDependent().indexOf("|"));
			if(afterData.length()>1)
			{
				afterData=afterData.substring(1);
			}
			else
			{
				afterData="";
			}
			//String[] data = task.getDependent().split("|");
			
			 String[] after =afterData.split(",");
			 for(int i =0 ;i<after.length;i++)
				{
					if(Integer.parseInt(after[i])!=currentTaskId)
					{
						if(newAfter.equalsIgnoreCase(""))
						{
							newAfter=after[i];
						}
						else
						{
							newAfter=newAfter+","+after[i];
						}
					}
				}
			 task.setDependent(preData+"|"+newAfter);
		}
		
		taskServer.updateTask(task);
		
	}
	
	void updateCurrentTaskForDelete(int taskId,int constrainTaskId)
	{
		ProjTaskServiceImpl taskServer = new ProjTaskServiceImpl();
		ProjectTaskDTO task = taskServer.getTaskByTaskId(taskId);
		String newPre="";
		if(task.getDependent()!=null&&task.getDependent().length()>0)
		{
			String preData =  task.getDependent().substring(0, task.getDependent().indexOf("|"));
			String afterData = task.getDependent().substring(task.getDependent().indexOf("|"));
			if(afterData.length()>1)
			{
				afterData=afterData.substring(1);
			}
			else
			{
				afterData="";
			}
			//String[] data = task.getDependent().split("|");
			
			 String[] pre =preData.split(",");
			 for(int i =0 ;i<pre.length;i++)
				{
					if(Integer.parseInt(pre[i])!=constrainTaskId)
					{
						if(newPre.equalsIgnoreCase(""))
						{
							newPre=pre[i];
						}
						else
						{
							newPre=newPre+","+pre[i];
						}
					}
				}
			 task.setDependent(newPre+"|"+afterData);
		}
		
		taskServer.updateTask(task);
		
	}
	@Override
	public void deleteConstrain(String constrainId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			TaskConstrain constrain = pm.getObjectById(TaskConstrain.class, constrainId);

			
			if (constrain != null) {
				updateCurrentTaskForDelete(constrain.getForTaskId(),constrain.getConstrainTaskId());
				updateConstrainTaskForDelete(constrain.getConstrainTaskId(),constrain.getForTaskId());
				pm.deletePersistent(constrain);
			}
		} finally {
			pm.close();
		}
	}
	@Override
	public void deleteListConstrain(List <TaskConstrainDTO> list) {
		// TODO Auto-generated method stub
		if(list!=null&&list.size()>0)
		{
			for(int i=0;i<list.size();i++)
			{
				deleteConstrain(list.get(i).getId());
			}
				
		}
	}
	
	public void deleteAllConstrainOfTask(ProjectTaskDTO data,List<ProjectTaskDTO> listTask) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			ProjectTask task = pm.getObjectById(ProjectTask.class, data.getId());
			for(TaskConstrain constrain:task.getListOfConstrain())
			{
				if (constrain != null) {
					updateCurrentTaskForDelete(constrain.getForTaskId(),constrain.getConstrainTaskId(),listTask);
					updateConstrainTaskForDelete(constrain.getConstrainTaskId(),constrain.getForTaskId(),listTask);
					pm.deletePersistent(constrain);
				}
			}
			//data=task.toDTO();
		}
		catch(Exception ex)
		{
			System.out.println(ex.getMessage());
		}finally {
			pm.close();
		}
	}
	

	void updateCurrentTaskForDelete(int taskId,int constrainTaskId,List<ProjectTaskDTO> listTask)
	{
		ProjTaskServiceImpl taskServer = new ProjTaskServiceImpl();
		ProjectTaskDTO task = taskServer.getFromList(taskId, listTask);
		String newPre="";
		if(task.getDependent()!=null&&task.getDependent().length()>0)
		{
			String preData =  task.getDependent().substring(0, task.getDependent().indexOf("|"));
			String afterData = task.getDependent().substring(task.getDependent().indexOf("|"));
			if(afterData.length()>1)
			{
				afterData=afterData.substring(1);
			}
			else
			{
				afterData="";
			}
			//String[] data = task.getDependent().split("|");
			
			 String[] pre =preData.split(",");
			 for(int i =0 ;i<pre.length;i++)
				{
					if(Integer.parseInt(pre[i])!=constrainTaskId)
					{
						if(newPre.equalsIgnoreCase(""))
						{
							newPre=pre[i];
						}
						else
						{
							newPre=newPre+","+pre[i];
						}
					}
				}
			 task.setDependent(newPre+"|"+afterData);
		}
		
		taskServer.updateTask(task);
		
	}
	
	void updateConstrainTaskForDelete(int taskId,int currentTaskId,List<ProjectTaskDTO> listTask)
	{
		ProjTaskServiceImpl taskServer = new ProjTaskServiceImpl();
		ProjectTaskDTO task = taskServer.getFromList(taskId, listTask);
		String newAfter="";
		if(task.getDependent()!=null&&task.getDependent().length()>0)
		{
			String preData =  task.getDependent().substring(0, task.getDependent().indexOf("|"));
			String afterData = task.getDependent().substring(task.getDependent().indexOf("|"));
			if(afterData.length()>1)
			{
				afterData=afterData.substring(1);
			}
			else
			{
				afterData="";
			}
			//String[] data = task.getDependent().split("|");
			
			 String[] after =afterData.split(",");
			 for(int i =0 ;i<after.length;i++)
				{
					if(Integer.parseInt(after[i])!=currentTaskId)
					{
						if(newAfter.equalsIgnoreCase(""))
						{
							newAfter=after[i];
						}
						else
						{
							newAfter=newAfter+","+after[i];
						}
					}
				}
			 task.setDependent(preData+"|"+newAfter);
		}
		
		taskServer.updateTask(task);
		
	}
	


	 
}
